package com.kzw.oa.system.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kzw.core.service.hibernate.DefaultEntityManager;
import com.kzw.oa.system.model.GlobalType;

@Service
@Transactional
public class GlobalTypeService extends DefaultEntityManager<GlobalType, Long> {

	@Transactional(readOnly = true)
	public List<GlobalType> getByParentIdCatKey(Long parentId, String catKey) {
		String hql = "from GlobalType gt where gt.parentId = ? and gt.catKey = ? order by gt.sn asc";
		return findByHQL(hql, parentId, catKey);
	}

	/**
	 * 删除分类，同时删除其下所有子子分类
	 */
	public void mulDel(Long proTypeId) {
		GlobalType globalType = get(proTypeId);
		String hql = "from GlobalType gt where gt.path like ?";
		List<GlobalType> subList = findByHQL(hql, globalType.getPath()+"%");

		for (GlobalType gt : subList) {
			remove(gt);
		}
	}
	
	/**
	 * 根据proTypeId删除下面所有节点的信息
	 */
	public void delChildrens(Long proTypeId) {
		GlobalType type = get(proTypeId);
		String hql = "delete from GlobalType vo where vo.path like ? or vo.proTypeId = ?";
		int count = getEntityDao().batchExecute(hql, type.getPath()+"%", proTypeId);
		logger.debug("删除数据操作成功，受影响的行数：" + count);
	}

	public Integer getCountsByParentId(Long parentId) {
		String hql = " select count(proTypeId) from GlobalType gt ";
		if (parentId != null && parentId != 0) {
			hql += " where gt.parentId=" + parentId;
		} else {
			hql += " where gt.parentId is null";
		}

		Object obj = getEntityDao().findUnique(hql);
		return Integer.parseInt(obj.toString());

	}
}
